/**
 * Created by ACER on 2018/1/30.
 */
app.controller('classification', function ($http, $scope) {
    $scope.classLists = [];
    //默认显示课程列表
    $scope.getFirstType = function (page) {
        $http({
            method: 'get',
            url: app.netPath + '/classtype/firsttype'
        }).then(function (res) {
            console.log(res.data);
            if (!res.data.error) {
                //一级类型数组
                $scope.firstTypeList = res.data.data;
                //设置默认一级类型选中对象
                $scope.firstType = res.data.data[0];
                //初始化二级类型
                $http({
                    method: 'get',
                    url: app.netPath + '/classtype/secondtype',
                    params: {'pid': $scope.firstType.typeId}
                }).then(function (res) {
                    console.log(res.data);
                    //二级类型数组
                    $scope.secondTypeList = res.data.data;

                    //设置默认二级类型选中对象
                    $scope.secondType = res.data.data[0];
                    //初始化三级类型
                    $http({
                        method: 'get',
                        url: app.netPath + '/classtype/secondtype',
                        params: {'pid': $scope.secondType.typeId}
                    }).then(function (res) {
                        console.log(res.data);
                        //三级类型数组
                        $scope.thirdTypeList = res.data.data;
                        //设置默认课程类型id
                        $scope.classTypeId = res.data.data[0].typeId;
                        //获得默认课程列表
                        $scope.classLists = [];
                        // alert($scope.classTypeId.typeId);
                        $scope.showClassList(page, $scope.classTypeId);


                    })


                });

            }
        });

    };
    //点击一级目录时获得课程列表
    $scope.getSecondType = function (page) {
        $http({
            method: 'get',
            url: app.netPath + '/classtype/secondtype',
            params: {'pid': $scope.firstType.typeId}
        }).then(function (res) {
            $scope.chosedIndex1 = 0;//默认是0使第一个有样式
            $scope.chosedIndex2 = 0;//默认是0使第二个有样式
            console.log(res.data);
            //二级类型数组
            $scope.secondTypeList = res.data.data;
            //设置默认二级类型选中对象
            $scope.secondType = res.data.data[0];
            //初始化三级类型
            $http({
                method: 'get',
                url: app.netPath + '/classtype/secondtype',
                params: {'pid': $scope.secondType.typeId}
            }).then(function (res) {
                //不为空
                if (res.data.data.length != 0) {
                    console.log(res.data);
                    $scope.thirdTypeList = res.data.data;
                    //设置默认课程类型id
                    $scope.classTypeId = res.data.data[0].typeId;
                    //获得默认课程列表
                    $scope.classLists = [];
                    // alert($scope.classTypeId.typeId);
                    $scope.showClassList(page, $scope.classTypeId);
                } else {
                    //为空
                    $scope.thirdTypeList = [];
                    $scope.classLists = [];
                }
            })

        })
    };
    $scope.chosedIndex1 = 0;//默认是0使第一个有样式
    //点击二级目录时获得课程列表
    $scope.getThirdType = function (typeId, index, page) {
        var infiniteScrollPreloader = $('.infinite-scroll-preloader');
        $scope.chosedIndex2 = 0;
        $scope.chosedIndex1 = index;
        $http({
            method: 'get',
            url: app.netPath + '/classtype/secondtype',
            params: {'pid': typeId}
        }).then(function (res) {
            //不为空
            if (res.data.data.length != 0) {
                console.log(res.data);
                $scope.thirdTypeList = res.data.data;
                //设置默认课程类型id
                $scope.classTypeId = res.data.data[0].typeId;
                //获得默认课程列表
                $scope.classLists = [];
                $scope.showClassList(page, $scope.classTypeId);
            } else {
                //为空
                $scope.thirdTypeList = [];
                $scope.classLists = [];
                // 删除加载提示符
                infiniteScrollPreloader.hide();
            }
        })
    };

    //点击三级目录时获得课程列表
    $scope.chosedIndex2 = 0;
    $scope.getClass = function (typeId, index, page) {
        //添加样式
        $scope.chosedIndex2 = index;
        $scope.classTypeId = typeId;
        $scope.classLists = [];
        $scope.showClassList(page, typeId);


    };

    var infiniteScroll = $('.infinite-scroll');
    var infiniteScrollPreloader = $('.infinite-scroll-preloader');
    //分页展示课程列表函数
    $scope.showClassList = function (page, typeId) {
        // 显示加载提示符
        infiniteScrollPreloader.show();
        $scope.page = page ? page : 1;

        // 若该加载为初始化加载，即没有传入page参数
        if (!page) {
            $scope.page = 1;
        }
        $http({
            method: 'get',
            url: app.netPath + '/class/atype',
            params: {
                'classTypeId': typeId,
                'page': $scope.page
            }
        }).then(function (res) {
            if (!res.data.error) {

                $scope.classList = res.data.data;
                for (var i = 0; i < $scope.classList.length; i++) {
                    if ($scope.classList[i].classImg) {
                        $scope.classList[i].classImg = app.netPath + '/resources/classImg/' + $scope.classList[i].classImg;
                        app.buildImageUrl($scope.classList[i], 'classImg', function () {
                            $scope.$digest();
                        });
                    }
                    //设置星星
                    $scope.postStar = [];
                    for (var j = 0; j < $scope.classList[i].classEvaluateGrade; j++) {
                        var postStar = '';
                        $scope.postStar.push(postStar);
                    }
                    $scope.classList[i].postStar = $scope.postStar;
                    $scope.classLists.push($scope.classList[i]);
                }
                console.log($scope.classLists);
                $scope.isOnloading = false;
                //当前页
                $scope.thispage = $scope.classLists.length % 10 == 0 ? $scope.classLists.length / 10 : $scope.classLists.length / 10 + 1;
                $scope.thispage = parseInt($scope.thispage);

                // 启用无限加载事件
                $.attachInfiniteScroll(infiniteScroll);

                console.log($scope.thispage);
                if (res.data.allPage <= $scope.thispage) {

                    // 加载完毕，则注销无限加载事件，以防不必要的加载
                    $.detachInfiniteScroll(infiniteScroll);
                    // 删除加载提示符
                    infiniteScrollPreloader.hide();
                }
            }else {
                $scope.classLists = [];
                // 删除加载提示符
                infiniteScrollPreloader.hide();
            }


        })
    };

    //获得课程详情
    $scope.getClassInfo = function (classId) {
        sessionStorage.setItem('classId', classId);
        location.href = '#xiangqing';
    };
    //跳转检索页面
    $scope.keyWord='';
    $scope.getSearch = function () {
        var keyWord=$scope.keyWord;
        if (keyWord != null && keyWord != '') {
            sessionStorage.setItem('keyWord', keyWord);
            location.href = '#search'
        }else {

        }
    }

});